package com.jimmy.reggie.conf;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redistemplate:
 * Performs automatic serialization/deserialization between the given objects
 * and the underlying binary data in the Redis store.
 * By default, it uses Java serialization for its objects
 * (through JdkSerializationRedisSerializer ).
 * For String intensive operations consider the dedicated StringRedisTemplate.
 * The central method is execute,
 * supporting Redis access code implementing the RedisCallback interface.
 * It provides RedisConnection handling such that neither
 * the RedisCallback implementation nor
 * the calling code needs to explicitly care about
 * retrieving/closing Redis connections, or handling Connection lifecycle exceptions.
 * For typical single step actions, there are various convenience methods.
 * Once configured, this class is thread-safe.
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<Object, Object> objectObjectRedisTemplate = new RedisTemplate<>();
        objectObjectRedisTemplate.setKeySerializer(new StringRedisSerializer());
        objectObjectRedisTemplate.setConnectionFactory(redisConnectionFactory);
        return objectObjectRedisTemplate;
    }
}
